# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import os

from itemadapter import ItemAdapter


class XiaoshuoPipeline:
    def process_item(self, item, spider):
        dir = "book\\" + item['xiaoshuo_topic'] + "\\" + item['xiaoshuo_name']
        if not os.path.exists(dir):
            os.makedirs(dir)
        # 章节有可能有空格，存文件不允许有空格字符，所以要做下处理
        filename = dir + "\\" + item['xiaoshuo_chapter'].replace('', '_') + ".txt"
        with open(filename, 'w', encoding="utf-8") as f:
            f.write("".join(item['xiaoshuo_content']))
        print('ok')
        return item
